package com.jsnkj.basic.activity

import androidx.lifecycle.MutableLiveData
import com.jsnkj.basic.bean.ErrorEntity
import com.jsnkj.basic.bean.WithdrawalsEntity
import com.jsnkj.basic.http.HttpApiService
import com.jsnkj.basic.mvvm.HttpViewModel
import com.jsnkj.basic.util.jsonToString
import com.yang.http.HttpRequest
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody.Companion.toRequestBody
import java.util.TreeMap

class WithDrawViewModel : HttpViewModel() {


    var moneyListResult = MutableLiveData<ArrayList<WithdrawalsEntity>>()

    val moneyList = ArrayList<WithdrawalsEntity>()

    fun postMoneyList() {

        launchHttp({
            HttpRequest.getService(HttpApiService::class.java).postAppSetting()
        }, onResult = { it ->
            var list = it.withdrawals
            if (list.isNullOrEmpty()) {
                list = ArrayList()
            }
            moneyList.addAll(list)
            moneyList[0].isSelect = true
            moneyListResult.value = moneyList

        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
        }) {
        }
    }


    var postApplyResult = MutableLiveData<String>()

    var errorResult: MutableLiveData<ErrorEntity> = MutableLiveData()

    /**
     * 用户领取红包奖励
     */
    fun postWithdrawalApply(id: String,deviceId:String,deviceType:String) {
        launchHttp({
            val postMap = TreeMap<String, Any> { p0, p1 -> p0.compareTo(p1) }
            postMap["wid"] = id
            postMap["deviceId"] = deviceId
            postMap["deviceType"] = deviceType

            HttpRequest.getService(HttpApiService::class.java).postWithdrawalApply(
                com.jsnkj.basic.util.AESUtils.encrypt(postMap.jsonToString()).jsonToString()
                    .toRequestBody("application/json;charset=UTF-8".toMediaTypeOrNull())
            )
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("提现申请已提交 请求成功：$it")
            postApplyResult.value = "提现申请已提交"
        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
        })
    }

    var huifuOauthRepsBean: com.jsnkj.basic.bean.HuifuOauthRepsBean =
        com.jsnkj.basic.bean.HuifuOauthRepsBean();
    fun getHuifubafangWithdrawOauth(listener: com.jsnkj.basic.adlistener.HuifuOauthListener){
        if(huifuOauthRepsBean.is_auth == null || "0".equals(huifuOauthRepsBean.is_auth)){
            launchHttp({
                HttpRequest.getService(HttpApiService::class.java).getHuifubafangOauth()
            }, onResult = { it ->
                huifuOauthRepsBean = it
                com.jsnkj.basic.util.LogUtils.e("获取授权接口成功：$it")
                listener.onOauthSuccess(huifuOauthRepsBean)
            }, onFailed = { code, msg ->
                errorResult.value = ErrorEntity.getInstance(code, msg)
                com.jsnkj.basic.util.ToastUtils.show("汇付授权接口调取失败")
            })
        }else{
            listener.onOauthSuccess(huifuOauthRepsBean)
        }

    }


    var postBindPayResult = MutableLiveData<String>()
    /**
     * 绑定支付宝
     */
    fun postWithdrawalApply(aliLoginId: String, realName: String) {
        launchHttp({
            val postMap = TreeMap<String, Any> { p0, p1 -> p0.compareTo(p1) }
            postMap["ali_login_id"] = aliLoginId
            postMap["real_name"] = realName

            HttpRequest.getService(HttpApiService::class.java).postUserAliInfo(
                com.jsnkj.basic.util.AESUtils.encrypt(postMap.jsonToString()).jsonToString().toRequestBody("application/json;charset=UTF-8".toMediaTypeOrNull())
            )
        }, onResult = { it ->
            com.jsnkj.basic.util.LogUtils.e("绑定支付宝 请求成功：$it")
            postBindPayResult.value = "绑定成功"
        }, onFailed = { code, msg ->
            errorResult.value = ErrorEntity.getInstance(code, msg)
        })
    }
}